/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package core;

import java.util.List;
import model.Dapstructure;
import model.Department;
import model.Employee;
import model.Processowner;
import model.Stepvalidation;
import org.hibernate.Session;
import org.hibernate.Transaction;
import util.ProjetHibernateUtil;

/**
 *
 * @author phinguyen
 */
public class ProcessOwnerCore {
    
     private static Session session;
    
    /**
     * constructor 
     * obtention de la session qui synchronisra les objets
     */
    public ProcessOwnerCore(){
        this.session=ProjetHibernateUtil.getSessionFactory().openSession();
    }
//    public static Session getSession(){
//        if(ProcessOwnerCore.session==null){
//            ProcessOwnerCore.session=ProjetHibernateUtil.getSessionFactory().openSession();
//        }
//         return ProcessOwnerCore.session;
//    }

    public List getAllSteps(){
    return this.session.getNamedQuery("lstStepValidation").list();
}
    public List<Department> getAllDepartment(){
        return this.session.getNamedQuery("HQL_ALL_DEPARTMENT").list();
    }
    public void saveDAP(Dapstructure dap){
        this.session.beginTransaction();
        this.session.save(dap);
        this.session.getTransaction().commit();
    }
    
      public void saveProcessOwner(Processowner po){
        this.session.beginTransaction();
        this.session.save(po);
        this.session.getTransaction().commit();
    }
    
      
    public void saveStep(String sname, Department department){
        Stepvalidation newstep=new Stepvalidation(sname, department);
        Transaction tr= session.getTransaction();
        tr.begin();
        session.saveOrUpdate(newstep);
        tr.commit();
    }
    
    
    public void saveDepartment(String dname, String ddescription, Employee director){
        Department newdepartment=new Department(dname, ddescription, director);
        Transaction tr= session.getTransaction();
        tr.begin();
        session.saveOrUpdate(newdepartment);
        tr.commit();
    }
    public void saveDepartmentobjet(Department dpt){
        Transaction tr= session.getTransaction();
        tr.begin();
        session.saveOrUpdate(dpt);
        tr.commit();
    }
    
    public Employee getEmployee(Integer empId){
//         return (Employee) this.session.getNamedQuery("HQL_EMPLOYEE_ID").setParameter("id", empId).uniqueResult();
           return (Employee)this.session.load(Employee.class,empId);
    }
    public List<Employee> getAllEmployee(){
        return this.session.getNamedQuery("HQL_ALL_EMPLOYEE").list();             
    }
    
    public static void main(String[] args){
        ProcessOwnerCore poc= new ProcessOwnerCore();
        
//        for(int i=0; i<getAllEmployee().size();i++){
//            System.out.println(getAllEmployee().get(i).getEid());
//        }
        System.out.println(poc.getEmployee(3));
//    
//        System.out.println(this.session.getAllDepartment().size());
        }
}
